<?php

/**
 * \file
 * Classe `DateTime`.
 */

namespace pilar\Entities /* \cond */ ; /* \endcond */
{

/**
 * Membre numérique de DateTime.
 *
 * \since 12/02/2009
 * \author Salim TALEB <salim.taleb@gmail.com>
 */
class DateTime extends Member {

protected function loadDefaultParams() {
 parent::loadDefaultParams();

	$this->params->set(array(
  'name' => 'datetime',
  'caption' => 'datetime',
  'genre' => 'she',     
  'formClass' => '\\pilar\\SimpleFormMembers\\DateTime',
  'searchFormClass' => '\\pilar\\SearchFormMembers\\DateTime'
 ));

 $this->params->define(array(
  'defaultValue' => '' // Valeur par défaut.
 ));
}

protected function initialize() {
 parent::initialize();

 $this->addField(
  new \pilar\Entities\Fields\DateTime(array(
   'defaultValue' => $this->get('defaultValue')
  ))
 );
}

public function acceptsFieldValue($fieldName, $value) {
 return (preg_match('/^([1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]-[0-1][0-9]-[0-5][0-9]-[0-5][0-9])$/', $value) == 1 
						|| preg_match('/^([1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]-[0-2][0-3]-[0-5][0-9]-[0-5][0-9])$/', $value) == 1);
}

public function getFieldValueFromFormMember($fm, $f = '') {
 return $fm->Y.'-'.$fm->M.'-'.$fm->D.'-'.$fm->H.'-'.$fm->I.'-'.($fm->get('withSeconds')?$fm->S:'00');
}

public function getFormFieldValueFromObjectMember($om, $f = '') {
 $ov = $om->get();

 if($ov == '') return '';

	$Y = substr($ov, 0, 4);
	$M = substr($ov, 5, 2);
	$D = substr($ov, 8, 2);
	$H = substr($ov, 11, 2);
	$I = substr($ov, 14, 2);
	$S = substr($ov, 17, 2);

 switch($f) {
  case 'Y': return $Y; break; 
  case 'M': return $M; break;
  case 'D': return $D; break;
  case 'H': return $H; break; 
  case 'I': return $I; break;
  case 'S': return $S; break;
  default: return '??';
 }
}


} // Class.

} // Namespace.

?>
